package com.cyj.dream.file.controller.ds;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.file.model.GenDatasourceConf;
import com.cyj.dream.file.service.GenDatasourceConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 数据源表控制器
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.file.controller
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-26 14:28
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@ResponseResult
@RestController
@Api(value = "dsConfig", tags = "数据源表控制器")
@RequestMapping(value = "/file/dsConfig", name = "数据源表控制器")
public class GenDsConfController {

    @Autowired
    private GenDatasourceConfService datasourceConfService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param datasourceConf 数据源表
     * @return
     */
    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", dataType = "Page<GenDatasourceConf>", required = true),
            @ApiImplicitParam(name = "datasourceConf", value = "数据源表对象", dataType = "GenDatasourceConf", required = true),
    })
    @RequestMapping(value = "/page", name = "分页查询", method = RequestMethod.GET)
    public Page<GenDatasourceConf> getSysDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) {
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        return datasourceConfService.page(page, Wrappers.query(datasourceConf));
    }

    /**
     * 查询全部数据源
     *
     * @return
     */
    @ApiOperation(value = "查询全部数据源", httpMethod = "GET")
    @RequestMapping(value = "/list", name = "查询全部数据源", method = RequestMethod.GET)
    public List<GenDatasourceConf> list() {
        log.info("进入 查询全部数据源 控制器方法");
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        return datasourceConfService.list();
    }

    /**
     * 通过id查询数据源表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id主键", dataType = "Integer", required = true)
    })
    @RequestMapping(value = "/{id}", name = "通过id查询数据源表", method = RequestMethod.GET)
    public Map<String, Object> getById(@PathVariable("id") Integer id) {
        log.info("进入 通过id查询数据源表 控制器方法，参数为：{}", id);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (id == null || id == 0) {
            log.error("出现错误：方法执行失败失败！参数id缺失，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
        map.put("list", datasourceConfService.getById(id));
        return map;
    }

    /**
     * 新增数据源表
     *
     * @param datasourceConf 数据源表
     * @return R
     */
    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "datasourceConf", value = "数据源表", dataType = "GenDatasourceConf", required = true)
    })
    @PostMapping("/save")
    public boolean save(@RequestBody GenDatasourceConf datasourceConf) {
        log.info("进入 新增数据源表 控制器方法，参数为：{}", JSONObject.toJSONString(datasourceConf));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (ObjectUtils.isEmpty(datasourceConf)) {
            log.error("出现错误：方法执行失败失败！参数缺失，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
        return datasourceConfService.saveDsByEnc(datasourceConf);
    }

    /**
     * 修改数据源表
     *
     * @param conf 数据源表
     * @return R
     */
    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", dataType = "Page<GenDatasourceConf>", required = true),
            @ApiImplicitParam(name = "datasourceConf", value = "数据源表对象", dataType = "GenDatasourceConf", required = true),
    })
    @RequestMapping(value = "/updateById", name = "修改数据源表", method = RequestMethod.PUT)
    public boolean updateById(@RequestBody GenDatasourceConf conf) {
        log.info("进入 修改数据源表 控制器方法，参数为：{}", JSONObject.toJSONString(conf));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (ObjectUtils.isEmpty(conf)) {
            log.error("出现错误：方法执行失败失败！参数缺失，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
        return datasourceConfService.updateDsByEnc(conf);
    }

    /**
     * 通过id删除数据源表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除数据源表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id主键", dataType = "Integer", required = true)
    })
    @RequestMapping(value = "/removeById", name = "通过id删除数据源表", method = RequestMethod.DELETE)
    public boolean removeById(@RequestParam("id") Integer id) {
        log.info("进入 通过id删除数据源表 控制器方法，参数为：{}", id);
        if (id == null || id == 0) {
            log.error("出现错误：方法执行失败失败！参数id缺失，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
        return datasourceConfService.removeByDsId(id);
    }

}
